package com.example.myapplication.navigation.fragment_recover;

import android.content.DialogInterface;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;
import androidx.navigation.fragment.NavHostFragment;

import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.bean.ProductInfo;
import com.example.myapplication.databinding.FragmentProductMainBinding;
import com.example.myapplication.databinding.viewmodel.UserViewModel;
import com.example.myapplication.navigation.fragment.BaseLifeFragment;
import com.example.myapplication.utils.NameCreator;

/**
 * A simple {@link Fragment} subclass.
 * create an instance of this fragment.
 */
public class ProductMainFragmentLife extends BaseLifeFragment {

    FragmentProductMainBinding binding;

    ProductInfo productInfo = new ProductInfo();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = FragmentProductMainBinding.inflate(getLayoutInflater());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        UserViewModel userViewModel= new ViewModelProvider(Navigation.findNavController(getView()).getViewModelStoreOwner(R.id.revover_graph)).get(UserViewModel.class);
        initEvent();
    }

    private void initEvent() {
        binding.btnGoProductList.setOnClickListener(view -> {
                    NavHostFragment.findNavController(ProductMainFragmentLife.this).navigate(R.id.action_RMainFragment_to_productListFragment);
                }
        );
        binding.btnDownloadInfo.setOnClickListener(view -> download());
        binding.btnLoadInfo.setOnClickListener(view -> loadDataToView());
    }

    private void download() {
        productInfo.setName("苹果手机");
        productInfo.setPrice(5000.0);
        productInfo.setDes("苹果手机，世界很好用，打电话，上网，玩游戏，聊天统统都可以");
        loadDataToView();
    }

    private void loadDataToView() {
        binding.setProduct(productInfo);
    }

    private void showDialog() {
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(requireContext());
        normalDialog.setTitle("我是一个普通Dialog");
        normalDialog.setMessage("你要点击哪一个按钮呢?");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                    }
                });
        normalDialog.setNegativeButton("关闭",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
        // 显示
        normalDialog.show();
    }

    @Override
    public String initLogHead() {
        return NameCreator.getInstance().getName("ProductMainFragment", hashCode());
    }
}
